<template>
    <div>
        <div>
        </div>
        <div style="height: 10px 0">
            <el-button @click="headerAddUser()" icon=" el-icon-circle-plus-outline" type="primary"
                class="ml-5">新增</el-button>
        </div>
        <div class="pd-10">
            <el-table :data="tableData" border stripe :header-cell-class-name="headerBg" width="auto">
                <el-table-column type="index" width="50">
                </el-table-column>
                <el-table-column prop="name" label="语言">
                </el-table-column>
                <el-table-column prop="hits" label="点击数" width="200">
                </el-table-column>
                <el-table-column prop="count" label="题数量">
                </el-table-column>
                <el-table-column prop="remark" label="备注" />
                <el-table-column prop="createTime" label="创建时间">
                </el-table-column>
                <el-table-column width="150">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" circle @click="editUser(scope.row)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" circle
                            @click="deleteTopticType(scope.row.id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div style="padding: 10px 0;">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :page-sizes="[5, 10, 20, 30]" :current-page="pageNum" :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>

        <el-dialog :title="operation" :visible.sync="dialogFormVisible" width="20%">
            <el-form :model="form" size="small">
                <el-form-item label="语言名称" :label-width="formLabelWidth">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>

                <el-form-item label="备注" :label-width="formLabelWidth">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save()">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog :title="operation" :visible.sync="editFormVisible" width="20%">
            <el-form :model="form" size="small">
                <el-form-item label="语言名称" :label-width="formLabelWidth">
                    <el-input disabled v-model="form.name"></el-input>
                </el-form-item>

                <el-form-item label="备注" :label-width="formLabelWidth">
                    <el-input v-model="form.remark"></el-input>
                </el-form-item>

                <el-form-item label="点击数" :label-width="formLabelWidth">
                    <el-input v-model="form.hits"></el-input>
                </el-form-item>

                <el-form-item label="题库量" :label-width="formLabelWidth">
                    <el-input v-model="form.count"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    name: "TopicType",
    data() {
        return {
            tableData: [],
            pageSize: 10,
            pageNum: 1,
            total: 0,
            headerBg: "headerBg",
            formLabelWidth: "90px",
            dialogFormVisible: false,
            editFormVisible: false,
            operation: "",
            form: {
                id: "",
                name: "",
                remark: '',
                hits: "",
                count: "",
            },
        }
    },
    created() {
        this.load();
    },
    methods: {
        editUser(row) {
            this.operation = "编辑类型";
            this.form.id = row.id;
            this.form.name = row.name;
            this.form.remark = row.remark;
            this.form.hits = row.hits;
            this.form.count = row.count;
            this.editFormVisible = true;
        },
        headerAddUser() {
            this.operation = "新增题库类型";
            this.dialogFormVisible = true;

        },
        save() {
            this.request.post("/topic_type/add", this.form).then(res => {
                if (res.code == 200) {
                    if (this.form.id.length == 0) {
                        this.$message.success("新增成功");
                        this.dialogFormVisible = false;
                        this.load();
                    } else {
                        this.$message.success("修改成功");
                        this.editFormVisible = false;
                        this.load();
                    }
                }
                else {
                    this.$message.error(res.msg);
                }
            });



        },
        load() {
            this.request.get("/topic_type/listPage", {
                params: {
                    pageNumber: this.pageNum,
                    pageSize: this.pageSize,
                }
            }).then(res => {
                this.tableData = res.data.list;
                this.total = res.data.total;
            });
        },
        deleteTopticType(id) {
            this.$confirm("此操作将永久删除该, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                this.request.delete("/topic_type/delete/" + id).then(res => {
                    if (res.code == 200) {
                        this.$message.success("删除成功");
                        this.load();
                    }
                    else {
                        this.$message.error(res.msg);
                    }
                });
            }).catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消删除"
                });
                this.load();
            });
        },
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.load();
        },
        handleCurrentChange(pageNum) {
            this.pageNum = pageNum;
            this.load();
        },
    },
    watch: {
        dialogFormVisible: {
            handler() {
                this.form = {
                    id: "",
                    name: "",
                    remark: '',
                    hits: "",
                    count: "",
                };
            }
        }
    },
}
</script>
<style>
.headerBg {
    background: #eee !important;
}
</style>